﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AgendaManager.AgendaService;

namespace AgendaManager
{
    public partial class frmSeleccionPais : Form
    {
        public Pais Pais { get; set; } 
        
        private List<Pais> paises = new List<Pais>();

        public frmSeleccionPais()
        {
            InitializeComponent();
        }

        private void frmSeleccionPais_Load(object sender, EventArgs e)
        {
            paises = ConnectHelper.ws.RetrieveEntities(new Pais(), string.Empty).Cast<Pais>().ToList();

            gvPaises.DataSource = null;
            gvPaises.DataSource = paises;
            gvPaises.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            gvPaises.Columns["Id"].Visible = false;
        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            if (gvPaises.SelectedRows.Count == 1)
            {
                if (Pais == null)
                    Pais = new Pais();

                Pais = (Pais)gvPaises.SelectedRows[0].DataBoundItem;

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("Seleccione un solo País por favor.",
                                "Seleccionar País.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void btnAgregarPais_Click(object sender, EventArgs e)
        {
            using (frmNuevoPais f = new frmNuevoPais())
            {
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    paises = ConnectHelper.ws.RetrieveEntities(new Pais(), string.Empty).Cast<Pais>().ToList();

                    this.gvPaises.DataSource = null;
                    this.gvPaises.DataSource = paises;
                    gvPaises.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                    gvPaises.Columns["Id"].Visible = false;
                }
            }
        }

        private void btRemove_Click(object sender, EventArgs e)
        {
            if (gvPaises.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("¿Está seguro que desea eliminar el País del sistema?",
                                "Eliminar País.",
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.OK)
                {
                    if (Pais == null)
                    {
                        Pais = (Pais)gvPaises.SelectedRows[0].DataBoundItem;

                        bool elimino = ConnectHelper.ws.DeleteEntity(Pais);
                        if (elimino)
                        {
                            MessageBox.Show("El País ha sido eliminado exitosamente.",
                                "Eliminar País.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);

                            paises = ConnectHelper.ws.RetrieveEntities(new Pais(), string.Empty).Cast<Pais>().ToList();
                            gvPaises.DataSource = null;
                            gvPaises.DataSource = paises;
                            gvPaises.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                            gvPaises.Columns["Id"].Visible = false;
                        }
                        else
                        {
                            //No pudo eliminar
                            MessageBox.Show("No se pudo eliminar el País por favor intente nuevamente más tarde.",
                                "Eliminar País.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
